package com.springboot.cloud.demos.feign.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.web.exception.NoLoginException;
import com.springboot.cloud.common.web.exception.SystemInnerBusinessException;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.feign.service.BannerService;
import com.springboot.cloud.demos.feign.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
    @Autowired
    private UserService userService;

    public User getUser(HttpServletRequest request) {
        String tokenUser = request.getHeader("x-client-token-user");
        JSONObject jsonObject = JSON.parseObject(tokenUser);
        String userName = jsonObject.getString("user_name");
        Result<User> user = userService.queryByMobile(userName);
        if (user.isFail()) {
            throw new NoLoginException();
        } else if (user.getData()==null) {
            throw new NoLoginException();
        }
        return user.getData();
    }
}
